package _2021;

import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
import java.util.List;

/**
 * 修改 Maven 工程的磁盘目录名和 artifactId 不一致的问题
 * <p>
 * 自顶向下
 * <p>
 * TODO: 没实现递归
 *
 * @author chenyanhua
 * @date 2022/2/8 0008 21:14
 */
public class ChangeMavenProjectModuleName2 {

    private static final String START = "<artifactId>";
    private static final String END = "</artifactId>";

    public static void main(String[] args) throws Exception {
        String pathName = "D:\\openSource\\seata";

        Path rootPath = Paths.get(pathName);
        Path pomPath = rootPath.resolve("pom.xml");
        List<String> allLines = Files.readAllLines(pomPath, StandardCharsets.UTF_8);
        StringBuilder builder = new StringBuilder();
        boolean inModules = false;
        for (String line : allLines) {
            builder.append(whiteSpace(line));
            line = line.trim();
            String others = line;
            if (line.equals("<modules>")) {
                inModules = true;
            } else if (line.equals("</modules>")) {
                inModules = false;
            } else if (line.startsWith("<module>") && line.endsWith("</module>") && inModules) {
                final String dirName = line.substring("<module>".length(), line.length() - "</module>".length());
                final Path dirPath = rootPath.resolve(dirName);
                final int lastIndexOf = dirName.lastIndexOf("/");
                final String prefix = lastIndexOf == -1 ? "" : dirName.substring(0, lastIndexOf + 1);
                final String artifactId = readSubModuleArtifactId(dirPath);
                final String newDirName = prefix + artifactId;
                others = String.format("<module>%s</module>", newDirName);
                Files.move(dirPath, rootPath.resolve(newDirName));

                System.out.println(dirName + " ----> " + newDirName);
            }

            builder.append(others).append("\n");
        }
        Files.write(pomPath, builder.toString().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
    }

    private static String readSubModuleArtifactId(Path dir) throws Exception {
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir);
        Iterator<Path> iterator = directoryStream.iterator();
        boolean inParent = false;
        while (iterator.hasNext()) {
            Path next = iterator.next();
            String fileName = next.getFileName().toString();
            if (fileName.endsWith("pom.xml")) {
                List<String> allLines = Files.readAllLines(next, StandardCharsets.UTF_8);
                for (String line : allLines) {
                    line = line.trim();
                    if (line.equals("<parent>")) {
                        inParent = true;
                    } else if (line.equals("</parent>")) {
                        inParent = false;
                    } else if (line.startsWith(START) && line.endsWith(END) && !inParent) {
                        return line.substring(START.length(), line.length() - END.length());
                    }
                }
            }
        }
        return null;
    }

    private static String whiteSpace(String line) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < line.length(); i++) {
            if (line.charAt(i) == ' ') {
                builder.append(" ");
            } else {
                break;
            }
        }
        return builder.toString();
    }

}
